# -*- coding: utf-8 -*-
# @Time    : 2019/10/20 15:58
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : metaclass_test.py


# 类也是对象，type创建类的类
def create_class(name):
    if name == "user":
        class User:
            def __str__(self):
                return "user"

        return User
    elif name == "company":
        class Company:
            def __str__(self):
                return "company"

        return Company


# type动态创建类
def say(self):
    return "i am user"
    # return self.name


class Baseclass:
    def answer(self):
        return "i am baseclass"


User = type("User", (Baseclass,), {"name": "user", "say": say})  # 类名称， 基类， 属性


# 什么是元类，元类是创建类的类，对象 <- class(对象) <- type
class Metaclass(type):  # 控制实例化的过程
    def __new__(cls, *args, **kwargs):
        return super().__new__(cls, *args, **kwargs)


class User1(metaclass=Metaclass):
    # 控制实例化的过程
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return "user"


# python中类的实例化过程，会首先寻找metaclass，通过metaclass去创建user类
# type去创建类对象

if __name__ == '__main__':
    # MyClass = create_class("user")
    # my_obj = MyClass()
    # print(type(my_obj))
    my_obj = User1("b1")
    print(my_obj)
